﻿using ETA.DtoMappings;
using ETA.Entity;
using ETA.IService;
using Ninject;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ETA.UnitTest
{
    public class ReferrerLogServiceTest
    {
        private static IReferrerLogService _IReferrerLogService { set; get; }
        private static IUserService _IUserService { set; get; }

        [SetUp]
        public void Init()
        {
            DtoMapConfig.InitMap();
            IKernel kernel = new StandardKernel(new NinjectModuleBuild());
            _IReferrerLogService = kernel.Get<IReferrerLogService>();
            _IUserService = kernel.Get<IUserService>();
        }

        [Test]
        public void AddRefferLogTest()
        {
            var response = _IUserService.QueryUser(new QueryUserRequest
            {
                User = new User
                {
                    LoginName = "skyforce2008"
                }
            });

            if (response.Result.IsSuccess)
            {
                var referrer = response.Users.FirstOrDefault();
                var createLogResponse = _IReferrerLogService.CreateReferrerLog(new CreateReferrerLogRequest
                {
                    ReferrerLog = new ReferrerLog
                    {
                        UserId = "A001",
                        Referrer = referrer,
                        Point = 1000
                    }
                });

                Assert.True(createLogResponse.Result.IsSuccess);
            }
        }

        [Test]
        public void QueryRefferLogTest()
        {
            var count = 0;
            var queryLogResponse = _IReferrerLogService.QueryReferrerLogList(new QueryReferrerLogRequest
                {
                    UserId = "A001"
                }, out count);

            Assert.True(queryLogResponse.ReferrerLogs.Any());
        }

        [Test]
        public void ModifyRefferLogTest()
        {
            var count = 0;
            var queryLogResponse = _IReferrerLogService.QueryReferrerLogList(new QueryReferrerLogRequest
            {
                UserId = "A001"
            }, out count);

            var log = queryLogResponse.ReferrerLogs.FirstOrDefault();
            log.UsedPoint = 100;
            var modifyLogResponse = _IReferrerLogService.ModifyReferrerLog(new ModifyReferrerLogRequest
            {
                ReferrerLog = log
            });

            Assert.True(modifyLogResponse.Result.IsSuccess);
        }

    }
}
